package web.servlet;

import service.Impl.StationServiceImpl;
import service.StationService;
import web.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/9.
 */
@WebServlet("/stationservlet/*")
public class StationServlet extends BaseServlet {
    @RequestMapping("/query")
    public void query(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException{
        //设置编码
        request.setCharacterEncoding("utf-8");
        //得到参数
        String stationnum=request.getParameter("stationNum");
        String startstation=request.getParameter("startStation");
        String endstaton=request.getParameter("endStation");
        if(stationnum==null||"".equals(stationnum)){
            request.setAttribute("msg","车次不能为空");
           request.getRequestDispatcher("/query.jsp").forward(request,response);
            return;
        }

        if(startstation==null||"".equals(startstation)){
            request.setAttribute("msg","出发地不能为空");
            request.getRequestDispatcher("/query.jsp").forward(request,response);
            return;
        }
        if(endstaton==null||"".equals(endstaton)){
            request.setAttribute("msg","目的地不能为空");
            request.getRequestDispatcher("/query.jsp").forward(request,response);
            return;
        }
        //调用业务方法
        StationService ss=new StationServiceImpl();
        try {
            Map<String,Object> map=ss.queryAll(startstation,endstaton,stationnum);
            //将查询的结果放到session中
            request.getSession().setAttribute("MAP",map);
            //请求转发到处理显示的servlet
           request.getRequestDispatcher("/WEB-INF/views/show.jsp").forward(request,response);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
